package com.gxhmplan.news.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxhmplan.news.dao.NewsDao;
import com.gxhmplan.news.entity.News;
import com.gxhmplan.news.service.NewsService;
import com.gxhmplan.util.JsonResult;

@Controller
@RequestMapping("")
public class NewsController {
	
	@Resource
	NewsService newsService;
	@Resource
	NewsDao newsDao;
	
	@RequestMapping("/newsCompany.htm")
	public ModelAndView newsCompany(Integer classify,Integer page) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/newsCompany.jsp");
		if(null==page || "".equals(page)){
			page=1;
		}
		PageHelper.startPage(page, 6);//分页拦截器  显示条数
		List<Map<String,Object>> newsList = newsService.findNews(1);
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(newsList);
		mav.addObject("newsList", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "newsCompany.htm");//路径
		return mav;
	}
	@RequestMapping("/newsIndustry.htm")
	public ModelAndView newsIndustry(Integer classify,Integer page) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/newsIndustry.jsp");
		if(null==page || "".equals(page)){
			page=1;
		}
		PageHelper.startPage(page, 6);//分页拦截器  显示条数
		List<Map<String,Object>> newsList2 = newsService.findNews(2);
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(newsList2);
		mav.addObject("newsList2", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "newsIndustry.htm");//路径
		return mav;
	}
	@RequestMapping("/newsPolicy.htm")
	public ModelAndView newsPolicy(Integer classify,Integer page) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/show/newsPolicy.jsp");
		if(null==page || "".equals(page)){
			page=1;
		}
		PageHelper.startPage(page, 6);//分页拦截器  显示条数
		List<Map<String,Object>> newsList3 = newsService.findNews(3);
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(newsList3);
		mav.addObject("newsList3", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "newsPolicy.htm");//路径
		return mav;
	}
	@RequestMapping("/detailNews.htm")
	public ModelAndView detailNews(String id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("news", newsDao.findNewsById(id));
		mav.addObject("news1", newsDao.findFrontNewsById(id));
		mav.addObject("news2", newsDao.findAfterNewsById(id));
		newsService.updateNews(id);//浏览+1
		mav.setViewName("/WEB-INF/pages/show/detailNews.jsp");
		return mav;
	}
	
	////////////////////////后端///////////////////////
	@RequestMapping("/news.htm")
	public ModelAndView news() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/news/news.jsp");
		return mav;
	}
	@RequestMapping("/newsMangent.htm")
	public ModelAndView newsMangent(Integer page) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/news/newsMangent.jsp");
		Integer classify=null;
		if(null==page || "".equals(page)){
			page=1;
		}
		PageHelper.startPage(page, 15);//分页拦截器  显示条数
		List<Map<String,Object>> newsList = newsService.findNews(classify);
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(newsList);
		mav.addObject("newsList", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "newsMangent.htm");//路径
		return mav;
	}
	@RequestMapping("/addNews.htm")
	public ModelAndView addNews(News news) {
		ModelAndView mav = new ModelAndView("redirect:newsMangent.htm");
		newsService.addNews(news);
		return mav;
	}
	@RequestMapping("/deleteNews.htm")
	public ModelAndView deleteNews(String id) {
		ModelAndView mav = new ModelAndView("redirect:newsMangent.htm");
		newsDao.deleteNewsById(id);
		return mav;
	}
	////////////////////////////////////
	//上传图片
	@ResponseBody
	@RequestMapping(value="/uploadPhoto.do",method=RequestMethod.POST)
	public JsonResult uploadPhoto(MultipartFile file,HttpServletRequest request) {
		return new JsonResult(newsService.uploadPhoto(file, request));
	}
}
